package com.sparkseries.controller;

import com.sparkseries.common.util.domain.Result;
import com.sparkseries.domain.vo.SysLogVO;
import com.sparkseries.dto.SysLogDTO;
import com.sparkseries.service.SysLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 日志管理控制器。
 */
@RestController
@RequestMapping("/log")
@Tag(name = "日志管理")
public class SysLogController {

    @Resource
    private SysLogService sysLogService;

    /**
     * 修改日志级别或类型。
     *
     * @param logId    要修改的日志的 ID
     * @param logLevel 要修改的日志级别
     * @param logType  要修改的日志类型
     * @return 默认响应类，封装在 Result 类中
     */
    @PutMapping("/updateLog/{logId}")
    @Operation(summary = "修改日志级别或类型")
    @Parameters({
            @Parameter(name = "logLevel", description = "要修改的日志级别", required = true),
            @Parameter(name = "logType", description = "要修改的日志类型", required = true)
    })
    @PreAuthorize("")
    public Result<?> updateLog(@PathVariable("logId") Long logId,
                               @RequestParam("logLevel") String logLevel,
                               @RequestParam("logType") String logType) {
        return sysLogService.updateLog(logId, logLevel, logType);
    }

    /**
     * 日志列表
     *
     * @param logDTO 日志传输类
     * @param detail 模糊查找关键字
     * @return 日志响应类集合
     */
    @GetMapping("/listLog")
    @Operation(summary = "日志列表")
    @Parameter(name = "detail", description = "模糊查找关键字", required = true)
    @PreAuthorize("")
    public Result<List<SysLogVO>> listLog(@RequestBody SysLogDTO logDTO, @RequestParam("detail") String detail) {
        return sysLogService.listLog(logDTO,detail);
    }

    /**
     * 查找日志
     *
     * @param logId 日志id
     * @return 日志响应类
     */
    @GetMapping("/getLog/{logId}")
    @Operation(summary = "查找日志")
    @PreAuthorize("")
    public Result<SysLogVO> getLog(@PathVariable("logId") Long logId) {
        return sysLogService.getLog(logId);
    }

    /**
     * 导出日志
     *
     * @return 默认响应类，封装在 Result 类中
     */
    @GetMapping("/export")
    @Operation(summary = "导出日志")
    @PreAuthorize("")
    public Result<?> export() {
        return sysLogService.exportLogs();
    }
}